package com.wnxy.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.wnxy.admin.common.ResponseResult;
import com.wnxy.admin.common.handler.SysBusinessEnum;
import com.wnxy.admin.dto.ChargeRealTimeDto;
import com.wnxy.admin.dto.ChargingPileRealTimeDto;
import com.wnxy.admin.dto.ChargingStationDto;
import com.wnxy.admin.entity.ChargingPile;
import com.wnxy.admin.service.IChargingPileService;
import com.wnxy.admin.service.IChargingStationRealTimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.*;

/**
 * <p>
 * 充电站--实时监控模块 前端控制器
 * </p>
 *
 * @author nhj
 *
 */
@CrossOrigin
@Api(tags = "（实时监控）充电站接口")
@RestController
@RequestMapping("/ChargingStationRealTime")
public class ChargingStationRealTimeController {

    private final IChargingStationRealTimeService chargingStationRealTimeServiceImpl;
    private final IChargingPileService chargingPileServiceImpl;

    private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    private String lastSearchContext = "";

    public ChargingStationRealTimeController(IChargingStationRealTimeService chargingStationRealTimeServiceImpl, IChargingPileService chargingPileServiceImpl) {
        this.chargingStationRealTimeServiceImpl = chargingStationRealTimeServiceImpl;
        this.chargingPileServiceImpl = chargingPileServiceImpl;
    }

    //防抖（Debounce）处理
    @ApiOperation(value = "联想词搜索--实时监控模块")
    @GetMapping("queryKeyword")
    public ResponseResult queryKeyword(String searchContext){

        lastSearchContext = searchContext;

        // 使用防抖，300毫秒内只执行一次搜索
        Future<List<ChargingStationDto>> futureResult = scheduler.schedule(() -> {
            List<ChargingStationDto> allChargingStation =
                    chargingStationRealTimeServiceImpl.queryKeyword(lastSearchContext);


            return allChargingStation;
        }, 300, TimeUnit.MILLISECONDS);

        try {
            // 等待调度任务完成，并获取搜索结果
            List<ChargingStationDto> searchResult = futureResult.get();

            // 返回搜索结果
            return ResponseResult.ok(searchResult);
        } catch (InterruptedException | ExecutionException e) {
            // 处理异常
            e.printStackTrace();
            return ResponseResult.fail(SysBusinessEnum.SYS_BUSY);
        }
    }



    @ApiOperation(value = "获取电站ID获取电桩--设备状态")
    @GetMapping("getChargeById")
    public ResponseResult getChargeById(Long id,Integer pageNum, Integer pageSize,Integer pileSort) {

        PageInfo<ChargingPileRealTimeDto> chargeRealTim =
                chargingStationRealTimeServiceImpl.getChargeById(id,pageNum,pageSize,pileSort);

        return ResponseResult.ok(chargeRealTim);
    }



    /**
     * @author: nhj
     */
    @ApiOperation(value = "获取电站的电桩信息--设备状态")
    @GetMapping("getChargeRealTim")
    public ResponseResult getChargeRealTim(Long id) {

        ChargeRealTimeDto chargeRealTim = chargingStationRealTimeServiceImpl.getChargeRealTim(id);

        return ResponseResult.ok(chargeRealTim);
    }


    /**
     * @author: nhj
     */
    @ApiOperation(value = "获取所有充电站信息--实时监控模块")
    @GetMapping("getAllChargingStation")
    public ResponseResult getAllChargingStation() {

        List<ChargingStationDto> allChargingStation =
                chargingStationRealTimeServiceImpl.getAllChargingStation();

        return ResponseResult.ok(allChargingStation);
    }

    /**
     * @author: nhj
     */
    @ApiOperation(value = "获取所有充电桩--实时监控模块")
    @GetMapping("getAllChargingPile")
    public ResponseResult getAllChargingPile() {

        List<ChargingPile> list = chargingPileServiceImpl.list();

        return ResponseResult.ok(list);
    }



}
